//Dio封装核心
import 'package:dio/dio.dart';

import 'api_endpoints.dart';
import 'interceptors/auth_interceptor.dart';
import 'interceptors/my_log_interceptor.dart';
import 'interceptors/error_interceptor.dart';

class DioClient {
  static final DioClient _instance = DioClient._internal();

  late Dio _dio;

  factory DioClient() => _instance;

  DioClient._internal() {
    _dio = Dio(BaseOptions(
        baseUrl: ApiEndpoints.baseUrl,
        connectTimeout: const Duration(seconds: 10),
        receiveTimeout: const Duration(seconds: 15),
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json',
        }));

    // 添加拦截器（顺序重要！）
    _dio.interceptors.add(MyLogInterceptor());
    _dio.interceptors.add(AuthInterceptor());
    _dio.interceptors.add(ErrorInterceptor(_dio));
  }

  Dio get dio => _dio;

// 添加通用请求方法
  Future<Response> get(
    String path, {
    Map<String, dynamic>? queryParams,
    Options? options,
  }) async {
    return _dio.get(path, queryParameters: queryParams, options: options);
  }

  Future<Response> post(
    String path, {
    dynamic data,
    Options? options,
  }) async {
    return _dio.post(path, data: data, options: options);
  }
// 添加其他方法：put, delete, upload等
}
